(function(obj) {
 
	
	obj.config = {
		url: '', //链接地址
		
	};
 
	obj.init=function(config) {
		this.config = config;
		return this;
	};
 

	
	
	/**
	 * 连接webcocket
	 */
	obj.connect = function() {
 
		window.WebSocket = window.WebSocket || window.MozWebSocket;
		if(!window.WebSocket) { // 检测浏览器支持  
			this.onerror('Error: WebSocket is not supported .');
			return;
		}
		this.socket = new WebSocket(this.config.url); // 创建连接并注册响应函数  
		this.socket.onopen = function() {
			obj.onopen();
		};
		this.socket.onmessage = function(message) {
			obj.onmessage(message); 
		};
		this.socket.onclose = function() {
			obj.onclose();
			obj.socket = null; // 清理  
		};
		this.socket.onerror = function(errorMsg) {
			obj.onerror(errorMsg);
		}
		return this;
	}
 
	/**
	 * 消息发送
	 */
	obj.send = function(message) {
		if(this.socket && this.socket.readyState == 1) {
			this.socket.send(message);
			return true;
		}
		this.onerror('please connect to the server first !!!');
		return false;
	}
 
	obj.close = function() {
		if(this.socket != undefined && this.socket != null) {
			this.socket.close();
		} else {
			this.onerror("this socket is not available");
		}
	}
 
	/**
	 * 消息回調
	 * @param {Object} message
	 */
	obj.onmessage = function(message) {
 
	}
 
	/**
	 * 链接回调函数
	 */
	obj.onopen = function() {
		
	}
 
	/**
	 * 关闭回调
	 */
	obj.onclose = function() {
 
	}
 
	/**
	 * 异常回调
	 */
	obj.onerror = function() {
 
	}
	
	obj.pingToServer = function() {
        String.prototype.getBytes = function() {
            var bytes = [];
            for (var i = 0; i < this.length; i++) {
                var charCode = this.charCodeAt(i);
                var cLen = Math.ceil(Math.log(charCode)/Math.log(256));
                for (var j = 0; j < cLen; j++) {
                    bytes.push((charCode << (j*8)) & 0xFF);
                }
            }
            return bytes;
        };

        var payload = 'i';
        var buffer = new ArrayBuffer(payload.length);
        var intView = new Int8Array(buffer);
        for(var i = 0; i < intView.length; i++) {
            intView[i] = payload.getBytes()[i];
        }
        this.socket.send(intView);
        console.log("客户端已向服务器发送一个ping消息：【" + intView.toString() + "】");
    };
 
})(ws = {});